package com.ymatou.iapi.exp.testcase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.ymatou.iapi.exp.parameter.EProduct;
import com.ymatou.iapi.exp.parameter.GetProductCoinRateListBean;
import com.ymatou.iapi.exp.service.GetProductCoinRateListCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.ExpCallService;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.utils.TestDataManager;

/**
 * 获取商品金币倍数列表 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GetProductCoinRateList {
	private static GetProductCoinRateListBean getproductcoinratelistBean;
	private static GetProductCoinRateListCall getproductcoinratelistCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商品金币倍数列表");
	}

	@Before
	public void caseUp() {
		getproductcoinratelistBean = new GetProductCoinRateListBean();
		getproductcoinratelistCall = new GetProductCoinRateListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getproductcoinratelistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verifyData(Map pdata) {
		JsonObject result = getproductcoinratelistCall.getJsonObject().get("Data").getAsJsonObject();
		Logger.verifyEquals("true", result.get("success").toString(), "验证success");
		JsonObject ja = result.get("productCoinRates").getAsJsonObject();
		ja.entrySet().forEach(ordercoin -> {
			String pid = ordercoin.getKey();
			int rrate = ordercoin.getValue().getAsInt();
			// int rcoin=ordercoin.getAsJsonObject().get("coin").getAsInt();
			int prate = (int) pdata.get(pid);
			Logger.verifyEquals(prate, rrate, "验证rate");
		});

	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetProductCoinRateList_001() {
		Logger.start(true, "获取商品金币倍数列表成功,ispsp为true");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			int userId  = urrc.getUserid();
			int sellerid = 3383;
			getproductcoinratelistBean.setUserId(userId);
			List<EProduct> productList = new ArrayList<>();
			Map pdata = new HashMap<>();
			EProduct e = new EProduct();
			e.setIsPspSeller(true);
			e.setProductId(TestDataManager.getRandomUUID().toString());
			e.setSellerId(sellerid);
			productList.add(e);
			pdata.put(e.getProductId(), ExpCallService.pspRate);

			getproductcoinratelistBean.setProductList(productList);
			getproductcoinratelistCall.setData(getproductcoinratelistBean);
			getproductcoinratelistCall.callService();
			verifyData(pdata);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_GetProductCoinRateList_002() {
		Logger.start(true, "获取多个商品金币倍数列表成功");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			int userId  = urrc.getUserid();
			int sellerid = 3383;
			getproductcoinratelistBean.setUserId(userId);
			List<EProduct> productList = new ArrayList<>();
			Map pdata = new HashMap<>();
			for (int i = 0; i < 3; i++) {
				EProduct e = new EProduct();
				e.setIsPspSeller(true);
				e.setProductId(TestDataManager.getRandomUUID().toString());
				e.setSellerId(sellerid);
				productList.add(e);
				pdata.put(e.getProductId(), ExpCallService.pspRate);

			}
			getproductcoinratelistBean.setProductList(productList);
			getproductcoinratelistCall.setData(getproductcoinratelistBean);
			getproductcoinratelistCall.callService();
			verifyData(pdata);

		} catch (

		Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_GetProductCoinRateList_003() {
		Logger.start(true, "获取多个商品金币倍数列表成功,倍率不同");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			int userId  = urrc.getUserid();
			int sellerid = 3383;
			getproductcoinratelistBean.setUserId(userId);
			List<EProduct> productList = new ArrayList<>();
			Map pdata = new HashMap<>();
			for (int i = 0; i < 3; i++) {
				EProduct e = new EProduct();
				e.setIsPspSeller(true);
				e.setProductId(TestDataManager.getRandomUUID().toString());
				e.setSellerId(sellerid);
				productList.add(e);
				pdata.put(e.getProductId(), ExpCallService.pspRate);

			}
			EProduct e = new EProduct();
			e.setIsPspSeller(false);
			e.setProductId(TestDataManager.getRandomUUID().toString());
			e.setSellerId(3382);
			productList.add(e);
			pdata.put(e.getProductId(), 1);
			getproductcoinratelistBean.setProductList(productList);
			getproductcoinratelistCall.setData(getproductcoinratelistBean);
			getproductcoinratelistCall.callService();
			verifyData(pdata);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_GetProductCoinRateList_004() {
		Logger.start(true, "不输入userid当做新客处理");
		try {
			Integer userId = ExpCallService.OldUser;
			int sellerid=ExpCallService.OldSeller;
			Map pdata = new HashMap<>();
//			getproductcoinratelistBean.setUserId(userId);
			List<EProduct> productList = new ArrayList<>();
			EProduct e = new EProduct();
			e.setIsPspSeller(false);
			e.setProductId(TestDataManager.getRandomUUID().toString());
			e.setSellerId(sellerid);
			productList.add(e);
			pdata.put(e.getProductId(), 1);
			getproductcoinratelistBean.setProductList(productList);
			getproductcoinratelistCall.setData(getproductcoinratelistBean);
			getproductcoinratelistCall.callService();
			JsonObject result = getproductcoinratelistCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("true", result.get("success").toString(), "验证success");
			verifyData(pdata);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_GetProductCoinRateList_005() {
		Logger.start(true, "不输入productList");
		try {
			Integer userId = 20238699;
			int sellerid=288484;
			Map pdata = new HashMap<>();
			getproductcoinratelistBean.setUserId(userId);
			List<EProduct> productList = new ArrayList<>();
			EProduct e = new EProduct();
			e.setIsPspSeller(false);
			e.setProductId(TestDataManager.getRandomUUID().toString());
			e.setSellerId(sellerid);
			productList.add(e);
			pdata.put(e.getProductId(), 5);
//			getproductcoinratelistBean.setProductList(productList);
			getproductcoinratelistCall.setData(getproductcoinratelistBean);
			getproductcoinratelistCall.callService();
			JsonObject result = getproductcoinratelistCall.getJsonObject().get("Data").getAsJsonObject();
			Logger.verifyEquals("true", result.get("success").toString(), "验证success");
			Logger.verifyEquals("{}",result.get("productCoinRates").toString(), "验证productCoinRates数据为空");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetProductCoinRateList_006() {
		Logger.start(true, "获取商品金币倍数列表成功,ispsp为false");
		try {
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			// urb.setDeviceId(TestDataManager.getRandomUUID().toString());
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer userId = uc.getUserid();
			int sellerid = 3383;
			Map pdata = new HashMap<>();
			getproductcoinratelistBean.setUserId(userId);
			List<EProduct> productList = new ArrayList<>();
			EProduct e = new EProduct();
			e.setIsPspSeller(false);
			e.setProductId(TestDataManager.getRandomUUID().toString());
			e.setSellerId(sellerid);
			productList.add(e);
			pdata.put(e.getProductId(), 1);
			getproductcoinratelistBean.setProductList(productList);
			getproductcoinratelistCall.setData(getproductcoinratelistBean);
			getproductcoinratelistCall.callService();
			verifyData(pdata);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetProductCoinRateList_007() {
		Logger.start(true, "获取商品金币倍数列表成功,老用户5倍");
		try {
			Integer userId =  ExpCallService.OldUser;
			int sellerid= ExpCallService.OldSeller;
			Map pdata = new HashMap<>();
			getproductcoinratelistBean.setUserId(userId);
			List<EProduct> productList = new ArrayList<>();
			EProduct e = new EProduct();
			e.setIsPspSeller(false);
			e.setProductId(TestDataManager.getRandomUUID().toString());
			e.setSellerId(sellerid);
			productList.add(e);
			pdata.put(e.getProductId(), ExpCallService.oldRate);
			getproductcoinratelistBean.setProductList(productList);
			getproductcoinratelistCall.setData(getproductcoinratelistBean);
			getproductcoinratelistCall.callService();
			verifyData(pdata);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}